define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'member/index' + location.search,
                    add_url: 'member/add',
                    edit_url: 'member/edit',
                    del_url: 'member/del',
                    multi_url: 'member/multi',
                    import_url: 'member/import',
                    table: 'member',
                }
            });

            var table = $("#table");

            table.on('load-success.bs.table', function (e, data) {
                $('label[for="merchant_id"]').parent().css('display','none');
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                visible: false,
                showToggle: false,
                showColumns: false,
                search: false,
                showExport: false,
                escape: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'merchant_id', title: __('merchant_id'), operate: '=',visible: false},
                        //{field: 'openid', title: __('Openid'), operate: 'LIKE'},
                        {field: 'avatar', title: __('Avatar'), operate: 'LIKE', events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'card_num', title: __('Card_num'), operate: 'LIKE'},
                        {field: 'agent_data', title: __('Agent_data'), searchList: {"0":__('Agent_data 0'),"1":__('Agent_data 1'),"2":__('Agent_data 2')}, formatter: Table.api.formatter.normal},
                        {field: 'agent_id', title: __('Agent_id')},
                        {field: 'agentbind_time', title: __('Agentbind_time'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'merchant.name', title: __('Merchant.name')},
                        {field: 'merchantbind_time', title: __('Merchantbind_time'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table,
                            buttons: [
                                {
                                    name: 'see',
                                    title: __('查看'),
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    text: '查看',
                                    //icon: 'fa fa-folder-o',
                                    url: function (row, column) {
                                        var url = 'member/see?id=' + row.id;
                                        return url;
                                    },
                                },
                                {
                                    name: 'washlog',
                                    title: __('洗车记录'),
                                    classname: 'btn btn-xs btn-success btn-addtabs',
                                    text: '洗车记录',
                                    //icon: 'fa fa-folder-o',
                                    url: function (row, column) {
                                        var url = 'washlog/index?member_id=' + row.id;
                                        return url;
                                    },
                                },
                                {
                                    name: 'order',
                                    title: __('订单记录'),
                                    classname: 'btn btn-xs btn-success btn-addtabs',
                                    text: '订单记录',
                                    //icon: 'fa fa-folder-o',
                                    url: function (row, column) {
                                        var url = 'order/index?member_id=' + row.id;
                                        return url;
                                    },
                                },
                                {
                                    name: 'mycard',
                                    title: __('卡券记录'),
                                    classname: 'btn btn-xs btn-success btn-addtabs',
                                    text: '卡券记录',
                                    //icon: 'fa fa-folder-o',
                                    url: function (row, column) {
                                        var url = 'mycard/index?member_id=' + row.id;
                                        return url;
                                    },
                                },

                            ],
                            events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));

                //selecttype 切换redio的id 点击切换改变对应的参数
                var sende_data=$('input[name="row[sende_data]"]').val();
                $('input[name="row[agent_data]"]').click(function () {
                    sende_data=this.value;
                    //发送对象:来源:0=自然流量,1=分销商,2=卡经销商
                    var url="distributor/index";
                    var field="name";
                    if(sende_data==2) url="card/dealer/index";
                    $("#c-agent_id_text").data("selectPageObject").option.data = url;
                    $("#c-agent_id_text").data("selectPageObject").option.orderBy = [];
                    $("#c-agent_id_text").data("selectPageObject").option.showField= field;
                    $("#c-agent_id_text").data("selectPageObject").option.searchField= field;
                })
            }
        }
    };
    return Controller;
});
